#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <errno.h>
#include <unistd.h>
#include <signal.h>
#include <sys/ipc.h>
#include <sys/shm.h>


#include <arpa/inet.h>//这里面有uint8_t uint32_t的定义

#define SEND_OK 0xAA
//typedef unsigned char uint8_t;
//typedef unsigned uint32_t;
typedef struct {
    uint8_t head[3];
	uint8_t type;
	uint8_t snum;
	uint8_t temp[2];	 //温度	
	uint8_t hum[2];		 //湿度
	uint8_t x;			 //三轴信息
	uint8_t y;			 
	uint8_t z;			 
	uint32_t ill;		 //光照
	uint32_t bet;		 //电池电量
	uint32_t adc; 		 //电位器信息
}MSG;

int main (int argc, char *argv[])
{
    int shmid;
    MSG *pmsg;
    key_t key;
    key = ftok("/", 'a');
    if (key < 0)
    {
        perror("fail to ftok");
        exit(-1);
    }
    shmid = shmget(key, sizeof(MSG), IPC_CREAT | IPC_EXCL | 0777);//创建共享内存
    if (shmid < 0)
    {
        if (errno = EEXIST)
        {
            shmid = shmget(key, 0, 0777);
        }
        else
        {
            perror("fail to shmget");
            exit(-1);
        }
    }
    
    if ((pmsg = (MSG *)shmat(shmid, NULL, 0)) == (MSG *)-1)//映射共享内存
    {
        perror("fail to shmat");
        exit(-1);
    }
    puts("即将进入循环");
    while (1)
    {
		
       // if (pmsg->send_flag == SEND_OK)//服务器接收并刷新共享内存完毕
       // {
         //   pmsg->send_flag = 0;//清除标志
            printf("user:%uF,%uF,%u%%,%u%%,%um/(s*s),%um/(s*s),%um/(s*s),%uLUX,%umV,%umV\n", pmsg->temp[0], pmsg->temp[1], pmsg->hum[0], pmsg->hum[1], pmsg->x, pmsg->y, pmsg->z, pmsg->ill, pmsg->bet, pmsg->adc);
       // }
	   sleep(5);
    }
    return 0;
}









